import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'package:hzy_normal_tool/hzy_normal_tool.dart';
import '../components/common_extendeed_image_widget.dart';
import '../components/common_widgets.dart';

class NormalModuleUtils {
  /// 用于加载 package 模块图片
  static configPackagesImage({
    required String packagename,
    required String name,
  }) {
    return "packages/$packagename/$name";
  }

  static normalModuleImagePath({
    required String name,
  }) {
    return configPackagesImage(
      packagename: package,
      name: name,
    );
  }

  static const package = "hzy_common_module";
}

/// 配置结束刷新
configEndRefresh({
  int? type,
  PageState? state,
  EasyRefreshController? refreshController,
}) {
  IndicatorResult result = IndicatorResult.success;

  if (state == PageState.errorState ||
      state == PageState.erroronlyTotal ||
      state == PageState.errorshowRelesh) {
    result = IndicatorResult.fail;
  } else if (state == PageState.noMoreDataState && type == 2) {
    result = IndicatorResult.noMore;
  }
  if (refreshController != null) {
    if (type == 1) {
      refreshController.finishRefresh(result);
    } else if (type == 2) {
      refreshController.finishLoad(result);
    }
  }
  return state;
}

/// 加载网络图片
Widget conFigNetWorkImage({
  String? url,
  double? width,
  double? height,
  BorderRadius? borderRadius = BorderRadius.zero,
  String? placeholder,
  double? loadradius,
  BoxFit fit = BoxFit.fill,
}) {
  return CommonExtendedImageWidget.configImage(
    imageurl: url,
    width: width,
    height: height,
    borderRadius: borderRadius,
    fit: fit,
    loadradius: loadradius,
  );
}

// 展示默认加载动画
void showload({String? msg}) {
  EasyLoading.instance.backgroundColor = Colors.white.withOpacity(0);
  EasyLoading.instance.maskColor = Colors.white.withOpacity(0);
  EasyLoading.instance.indicatorColor = Colors.white.withOpacity(0);
  EasyLoading.instance.textColor = Colors.white.withOpacity(1);
  EasyLoading.instance.loadingStyle = EasyLoadingStyle.custom;
  EasyLoading.instance.radius = 12;
  EasyLoading.instance.contentPadding = EdgeInsets.symmetric(
    vertical: 15.0,
    horizontal: msg == null ? 20 : 30.0,
  );
  EasyLoading.instance.textPadding = EdgeInsets.zero;
  EasyLoading.instance.boxShadow = [
    BoxShadow(
      color: HzyNormalColorS.col000000.withOpacity(0),
    )
  ];
  EasyLoading.show(
    indicator: configLoadWidget(),
    maskType: EasyLoadingMaskType.custom,
    dismissOnTap: true,
    status: msg,
  );
}

/// 显示toast msg
/// 传content 后,如果没传ontap 会调用返回上一个界面的方法
/// type == 1 普通 toast , 2 成功toast 3 失败toast
void showtoastmsg(String msg,
    {int type = 1, BuildContext? context, Function()? ontap}) {
  EasyLoading.instance.loadingStyle = EasyLoadingStyle.dark;
  if (type == 1) {
    EasyLoading.showToast(msg);
  } else if (type == 2) {
    EasyLoading.showSuccess(msg);
  } else if (type == 3) {
    EasyLoading.showError(msg);
  }
  if (ontap != null || context != null) {
    Future.delayed(EasyLoading.instance.displayDuration).then((value) {
      if (ontap != null) {
        ontap();
      } else {
        Navigator.of(context!).pop();
      }
    });
  }
}

void shownormalloadmsg({String? msg}) {
  EasyLoading.instance.loadingStyle = EasyLoadingStyle.dark;
  EasyLoading.show(status: msg);
}

// 取消加载动画
void loaddismiss() {
  EasyLoading.dismiss();
}
